package compilers.node;

import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import org.antlr.runtime.Token;

import compilers.Node;
import compilers.output.ErrorOutput;
import compilers.stmt.Var;
import compilers.table.MemScope;
import compilers.table.Scope;

public class VarList extends Node {
	Vector<Var> fVars = new Vector<Var>();

	public void addVar(Var fVar, Scope scope, Token token) {		
		if (fVar.getfExpressions().size() > 1) {
			fVar.getfExpressions().removeElementAt(0);
			if (fVar.getfType().getfIdentifier().getIndices(token).isEmpty()) {
				new ErrorOutput(token, "Array initialization of non-array.");
			}
		} else {
			if (!fVar.getfType().getfIdentifier().getIndices(token).isEmpty()) {
				fVar.getfExpressions().removeElementAt(0);
				if (fVar.getfExpressions().size() > 0) {
					new ErrorOutput(token, "Array initializer must be an initializer list.");
				}
			}
		}
		scope.addVariable(fVar.getfType().getfIdentifier().getfIdentifier(), fVar.getfType(), token);
		fVars.add(fVar);
	}
	
	public void print(int tab) {
		for (int i=1;i<=tab;i++){System.out.print("\t");}System.out.print("|-");
		System.out.println("VarList");
		Enumeration<Var> enumeration = fVars.elements();
		while(enumeration.hasMoreElements()){
			((Node) enumeration.nextElement()).print(tab+1);
		} 
	}

	public void generate(BufferedWriter writer, MemScope scope) throws IOException {
		// NOTHING TO GENERATE
	}

}
